package cn.aitstone.org.controller;

import cn.aitstone.basic.annotation.PreAuthorize;
import cn.aitstone.basic.utils.AjaxResult;
import cn.aitstone.basic.utils.PageList;
import cn.aitstone.org.domain.Employee;
import cn.aitstone.org.query.EmployeeQuery;
import cn.aitstone.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 员工接口类
 * @Author: Sheldon
 * @Version: 1.0
 * @Date:  2022/6/24 11:07
 */
@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
    * @Title: loadById
    * @Description: 根据员工id查询
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/24 11:08
    * @Parameters: [id]
    * @Return cn.aitstone.org.domain.Employee
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee loadById(@ApiParam(value="部门表的主键",required = true) @PathVariable("id") Long id){
        return employeeService.loadById(id);

    }

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/24 11:08
    * @Parameters: []
    * @Return java.util.List<cn.aitstone.org.domain.Employee>
    */
    @GetMapping
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /**
    * @Title: remove
    * @Description: 根据id删除
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/24 11:47
    * @Parameters: [id]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据id删除员工", sn="employee:delete")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }

    }

    /**
    * @Title: addOrUpt
    * @Description: 新增和修改接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/24 11:47
    * @Parameters: [employee]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PutMapping
    @PreAuthorize(name = "保存和修改员工", sn="employee:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            } else{
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }

    }

    /**
    * @Title: queryData
    * @Description: 高级查询分页接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/24 11:48
    * @Parameters: [employeeQuery]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping
    @PreAuthorize(name = "获取员工列表", sn="employee:list")
    public PageList<Employee> queryData(@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> data = employeeService.queryData(employeeQuery);
        return data;

    }

    /**
    * @Title: batch
    * @Description: 批量删除
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/25 11:18
    * @Parameters:
    * @Return
    */
    @PatchMapping
    @PreAuthorize(name = "批量删除员工", sn="employee:batch")
    public AjaxResult batch(@RequestBody Long[] ids){
        try {
            employeeService.batchdel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }

    }

    @GetMapping("/list/{shopId}")
    public List<Employee> loadByShopId(@PathVariable("shopId") Long shopId){
        return employeeService.loadByShopId(shopId);

    }


}
